package com.harmontronics.hanma.paas.paas.consts;


import com.harmontronics.hanma.paas.paas.exception.ErrorKeyException;
import com.harmontronics.hanma.paas.paas.utils.StringUtil;
import liquibase.pro.packaged.B;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 错误码I18nKeyEnum
 */
public class BaseI18nKeyEnum {

    private static final Map<Integer, BaseI18nKeyEnum> ENUM_MAP = new ConcurrentHashMap<>();
    /**
     * 系统错误
     **/
    //系统异常
    public static final BaseI18nKeyEnum SYSTEM_ERROR = new BaseI18nKeyEnum(-1, "hm.paas.system_error");

    //请求参数错误
    public static final BaseI18nKeyEnum REQUEST_PARAM_ERROR = new BaseI18nKeyEnum(1001, "hm.paas.request_param_error");

    public static final BaseI18nKeyEnum FIELD_NON_NULLABLE = new BaseI18nKeyEnum(1006,"hm.pass.field_non_nullable");
    //请求参数错误
    public static final BaseI18nKeyEnum ERROR_CODE_EXISTS = new BaseI18nKeyEnum(1004, "hm.paas.error_code_exists");
    //查询数据库超时
    public static final BaseI18nKeyEnum DB_QUERY_TIMEOUT = new BaseI18nKeyEnum(1002, "hm.paas.db_query_timeout");
    //非法请求
    public static final BaseI18nKeyEnum REQUEST_INVALID = new BaseI18nKeyEnum(1003, "hm.paas.request_invalid");
    public static final BaseI18nKeyEnum ACCOUNT_PWD_LOCK_ERROR = new BaseI18nKeyEnum(1005, "hm.paas.account_pwd_lock_error");

    public static final BaseI18nKeyEnum NO_PERMISSION=new BaseI18nKeyEnum(1007,"hm.paas.no_permission");

    public static final BaseI18nKeyEnum FILESIZE_LONG = new BaseI18nKeyEnum(1008,"hm.paas.filesize_long");

    public static final BaseI18nKeyEnum WRITING_FILE_ERROR=new BaseI18nKeyEnum(1009,"hm.pass.writing_file_error");

    private Integer code;
    private String i18nKey;


    public BaseI18nKeyEnum(Integer code, String i18nKey) {
        if (ENUM_MAP.containsKey(code)) {
            throw new ErrorKeyException(ERROR_CODE_EXISTS);
        } else {
            this.code = code;
            this.i18nKey = i18nKey;
            ENUM_MAP.put(code, this);
        }

    }

    public Integer getCode() {
        return code;
    }

    public String getI18nKey() {
        return i18nKey;
    }


    /**
     * 通过key返回枚举
     *
     * @param i18nKey
     * @return
     */
    public static BaseI18nKeyEnum findByI18nKey(String i18nKey) {
        if (StringUtil.isNotEmpty(i18nKey)) {
            for (BaseI18nKeyEnum i18nKeyEnum : ENUM_MAP.values()) {
                if (i18nKey.equals(i18nKeyEnum.getI18nKey())) {
                    return i18nKeyEnum;
                }
            }
        }
        //查询不到就返回系统异常
        return SYSTEM_ERROR;
    }
}
